home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 856 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.9 KB

  1. From: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
  2. Message-ID: <4j6tgs$rhm@news.BelWue.DE>
  3. X-Original-Date: 25 Mar 1996 19:51:56 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 26 Mar 96 04:46:07 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: constness of private members and methods
  9. Organization: Fakultdt f|r Mathematik und Informatik
  10. References: <4j49e0$8fo@dub-news-svc-4.compuserve.com>
  11. Reply-To: dietmar.kuehl@uni-konstanz.de
  12. X-Newsreader: TIN [version 1.2 PL2]
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMVd2p+EDnX0m9pzZAQHqCAF/ZDO6HPcBVdic8NAEUIGCnsaWOyQKu1+G
  15.     N2prj3MkxNv3ceH1NeLoQJz61IT3Mo+k
  16.     =KXII
  17.  
  18. Hi,
  19.  
  20. Philippe Verdy (100105.3120@compuserve.com) wrote:
  21. [lengthy description of problems with bitwise instead of logical
  22. constness elided]
  23.  
  24. : Is there any way (or proposal) to explicitly specify members
  25. : of a class definition which are relaxed on constness certi-
  26. : fication ?
  27.  
  28. Yes: it is possible to declare a data member of an object to be
  29. 'mutable'.  This allows to modify this member even in a 'const'
  30. function. The main idea which leads to a reasonable use of 'mutable' is
  31. the concept of logical versus bitwise constness: In C++ bitwise
  32. constness is generally assumed. You cannot change the member of a
  33. constant object. However, sometimes this is not what you want (auxilary
  34. marks and cached values are two examples).  Instead, logical constness
  35. is desired: The representation of the object might change but the value
  36. represented stays the same. Note, however, that the language does not
  37. (and can not) enforce that logical constness is maintained if the
  38. object has mutable members. It is the responsibility of the programmer
  39. to ensure logical constness.
  40.  
  41. : Would not it be great if we could simply define such members
  42. : as "unconst" like in the following example :
  43.  
  44. :   class T {
  45. :   public:
  46. :     T()                   { ...; _mark = 0; }
  47. :     virtual ~T();
  48.  
  49. :     virtual void show() const;
  50. :   protected:
  51. :     void mark() const     { _mark = 1; } // RELAXED HERE
  52. :     void unmark() const   { _mark = 0; } // RELAXED HERE
  53. :     bool ismarked() const { return _mark; }
  54. :   private:
  55. :     unconst char _mark; // HERE IS WHERE THIS APPLIES
  56. :   }
  57.  
  58. If you replace 'unconst' by 'mutable' you have what will become
  59. standard. Since the 'mutable' keyword is a recent addition to the
  60. DWP, it is not yet supported by all compilers.
  61. --
  62. dietmar.kuehl@uni-konstanz.de
  63. http://www.informatik.uni-konstanz.de/~kuehl
  64. I am a realistic optimist - that's why I appear to be slightly pessimistic
  65. ---
  66. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  67. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  68. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  69. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  70. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  71.